local CommonOHOH = _G.CommonOHOH;
local M = CommonOHOH.class.new(script, CommonOHOH.files.BaseComponent());

function M.new(self,...)
	return M(self,...);
end;

function M.ctor(self, parent)
	M.super.ctor(self, parent);

    self.player=_G.client.local_player

    self.TitleLable=self.parent:WaitForChild_Loop("Enter.UI.SurfaceGui.ctrl.startText").Value
    self.PlayerCount=self.parent:WaitForChild_Loop("Enter.UI.SurfaceGui.ctrl.num").Value
    self.PlayerCell=self.parent:WaitForChild_Loop("Enter.UI.SurfaceGui.ctrl.cell").Value
    self.MaxPlayerCount=self.parent:WaitForChild_Loop("MaxPlayer").Value
    self.NowPlayerCount=0

    self.PlayerCell.Visible=false

    self.PlayerCellList={}
    self:Clearn()

    self.parent.PlayerAddEvent:Connect(function(player)
        if self.player.main_inst==player then
            self.player:GetComponent(CommonOHOH.load.Common_WeaponDropCtrl()):ShowLuckyInfo()
        end
        
         self:AddPlayer(player)
    end)


    self.parent.PlayerRemoveEvent:Connect(function(player)
        if self.player.main_inst==player then
            self.player:GetComponent(CommonOHOH.load.Common_WeaponDropCtrl()):CloseLuckyInfo()
        end
        self:RemovePlayer(player)
   end)

    self.parent.ClearReadyEvent:Connect(function()
            self:Clearn()
    end)

    self.parent.ReayTimeEvent:Connect(function(t)
        self:ReayTime(t)
    end)

end;


function M.dtor(self)
	M.super.dtor(self);
end;

function M.AddPlayer(self,player)
    if self.PlayerCellList[player] then
        return 
    end
    self.NowPlayerCount=self.NowPlayerCount+1
    self.PlayerCount.Text=self.NowPlayerCount.."/".. self.MaxPlayerCount
    self.PlayerCellList[player]=self:CreateCell(player)
end


function M.RemovePlayer(self,player)

    if not self.PlayerCellList[player] then
        return 
    end


    self.NowPlayerCount=self.NowPlayerCount-1
    self.PlayerCount.Text=self.NowPlayerCount.."/".. self.MaxPlayerCount
    self.PlayerCellList[player]:Destroy()
    self.PlayerCellList[player]=nil
end

function M.Clearn(self)

    self.NowPlayerCount=0
    self.PlayerCount.Text=self.NowPlayerCount.."/".. self.MaxPlayerCount
    self.TitleLable.Text="Enter The Zone To Play"
    for k,v in pairs(self.PlayerCellList) do
        v:Destroy()
    end
    self.PlayerCellList={}
end

function M.ReayTime(self,t)
    self.TitleLable.Text="("..t..")"
end

local PlayerInfoHelper=_G.ClientOHOH.load.PlayerInfoHelper()

function M.CreateCell(self,player)


   local ui= self.PlayerCell:Clone()
   ui.Visible=true
   ui.Parent=self.PlayerCell.Parent
   local HeadImage=CommonOHOH:GetPath_Loop("ctrl.img",ui).Value
   local NameLable=CommonOHOH:GetPath_Loop("ctrl.leve",ui).Value

   PlayerInfoHelper.GetPlayerInfo(player.UserId,function(h,n)
            HeadImage.Image=h
            NameLable.Text=n
    end)
    return ui
end

return M